package com.lkd.job;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyChannel;
import com.lkd.contract.SupplyContract;
import com.lkd.emq.MqttProducer;
import com.lkd.entity.ChannelEntity;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.service.ChannelService;
import com.lkd.service.VendingMachineService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.util.ShardingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SupplyAddJob {
    @Autowired
    private VendingMachineService vendingMachineService;

    @Autowired
    private ChannelService channelService;

    @Autowired
    private MqttProducer mqttProducer;

    @XxlJob("supplyAddHandler")
    public ReturnT<String> supplyAdd(String param) throws Exception{
        ShardingUtil.ShardingVO vo = ShardingUtil.getShardingVo();
        int index = vo.getIndex();
        int total = vo.getTotal();
        //搜索所有的运营状态的设备
        LambdaQueryWrapper<VendingMachineEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VendingMachineEntity::getVmStatus, VMSystem.VM_STATUS_RUNNING).apply("mod(id,"+total+" ) = "+index);
        List<VendingMachineEntity> list = vendingMachineService.list(wrapper);
        //抽出所有的设备编号
        List<String> list1 = new ArrayList<>();
        for (VendingMachineEntity vendingMachineEntity : list) {
            list1.add(vendingMachineEntity.getInnerCode());
        }
        //判断是否发送消息
        selectAndSend(list1);
        return ReturnT.SUCCESS;
    }

    /**
     * 判断是否需要补货并发送消息
     * @param list1
     */
    private void selectAndSend(List<String> list1) {
        SupplyContract supplyContract = new SupplyContract();
        boolean flag = true;
        for (String innerdode : list1) {
            List<SupplyChannel> supplyChannels = new ArrayList<>();
            List<ChannelEntity> channelEntities = channelService.list(new LambdaQueryWrapper<ChannelEntity>().eq(ChannelEntity::getInnerCode, innerdode));
            for (ChannelEntity channelEntity : channelEntities) {
                Integer maxCapacity = channelEntity.getMaxCapacity();
                Integer currentCapacity = channelEntity.getCurrentCapacity();
                int capacity = maxCapacity - currentCapacity;
                //判断是否需要补货
                if(capacity>0){
                    SupplyChannel supplyChannel = getSupplyChannel(channelEntity, capacity);
                    supplyChannels.add(supplyChannel);
                    flag = false;
                }
            }
            //如果需要补货,封装并发送消息
            if(flag == false) {
                supplyContract.setSupplyData(supplyChannels);
                supplyContract.setInnerCode(innerdode);
                String msg = JSON.toJSONString(supplyContract);
                mqttProducer.send(TopicConfig.TASK_SUPPLY_TOPIC,msg);
            }
        }
    }

    /**
     * 封装货道实体类
     * @param channelEntity
     * @param capacity
     * @return
     */
    private SupplyChannel getSupplyChannel(ChannelEntity channelEntity, int capacity) {
        SupplyChannel supplyChannel = new SupplyChannel();
        supplyChannel.setCapacity(capacity);
        supplyChannel.setChannelId(channelEntity.getChannelCode());
        supplyChannel.setSkuId(channelEntity.getSkuId());
        return supplyChannel;
    }
}
